package pox.sheet;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Sheet {
	String sheetName;
	FileOutputStream sheetFile;
	static String PATH = "E:/poiExcel/test/xl/worksheets/";
	/**
	 * 新建一个sheet
	 * 
	 * @param sheetName
	 *            sheet文件名称
	 */
	public Sheet(String sheetName) {
		this.sheetName = sheetName;
		try {
			File file=new File(PATH+ sheetName + ".xml");
			file.getParentFile().mkdirs();
			file.createNewFile();
			sheetFile = new FileOutputStream(PATH+ sheetName + ".xml");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 写入sheet文件头
	 * 
	 * @throws IOException
	 */
	public void start() throws IOException {
		String string = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" mc:Ignorable=\"x14ac\" xmlns:x14ac=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac\"><dimension ref=\"A1\"/><sheetViews><sheetView tabSelected=\"1\" workbookViewId=\"0\"><selection activeCell=\"K18\" sqref=\"K18\"/></sheetView></sheetViews><sheetFormatPr defaultRowHeight=\"13.5\" x14ac:dyDescent=\"0.15\"/><sheetData>";
		sheetFile.write(string.getBytes());
	}

	/**
	 * 创建当前sheet文件中的一行
	 * 
	 * @param index
	 * @return
	 */
	public Row createRow(Integer index) {
		return new Row(index, sheetFile);
	}

	/**
	 * 写入sheet尾部内容
	 * 
	 * @throws IOException
	 */
	public void end() throws IOException {
		String string = "</sheetData><phoneticPr fontId=\"1\" type=\"noConversion\"/><pageMargins left=\"0.7\" right=\"0.7\" top=\"0.75\" bottom=\"0.75\" header=\"0.3\" footer=\"0.3\"/><pageSetup paperSize=\"9\" orientation=\"portrait\" r:id=\"rId1\"/></worksheet>";
		sheetFile.write(string.getBytes());
		sheetFile.close();
	}
}
